package com.codinghero.zqlb.spider.bo;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.codinghero.util.DateUtils;
import com.codinghero.util.FileUtils;
import com.codinghero.util.StringUtils;
import com.codinghero.zqlb.match.LiveChannel;
import com.codinghero.zqlb.match.MatchUtils;
import com.codinghero.zqlb.match.SoccerMatch;

public class GenMatchPageBo {
	
	private static int COLUMN_NUM = 1;
	
	private MatchBo matchBo = new MatchBo();
	private LiveChannelBo liveChannelBo = new LiveChannelBo();
	
	public String genAllMatches() {
		return getMatchPageStr(matchBo.getAllValidMatches());
	}
	
	public String genEuroCupMatches() {
		return getMatchPageStr(new Integer[] { 200 });
	}
	
	public String genCslMatches() {
		return getMatchPageStr(new Integer[] { 10000 });
	}
	
	public String genNbaMatches() {
		return getMatchPageStr(new Integer[] { 100003 });
	}

	public String getMatchPageStr(Integer[] matchTypes) {
		return getMatchPageStr(matchBo.getMatchesByTypeIds(matchTypes));
	}
	
	public String getMatchPageStr(List<SoccerMatch> list) {
		
		StringBuilder sb = new StringBuilder();
		
		String prevDate = "1970年1月1日&nbsp;星期X";
		sb.append("<table>");
		sb.append(FileUtils.LINE_SEPARATOR);
		for (int i = 0, l = 0; i < list.size(); i++, l++) {
			SoccerMatch match = list.get(i);
			String curDate = 
					DateUtils.getDateStr(match.getStartDate(), "yyyy年M月d日")
					+ "&nbsp;"
					+ DateUtils.getDateStr(match.getStartDate(), "E");
			String trClass = null, typeName = null, team1Name = null, team2Name = null;

			// another new day
			if (!prevDate.equals(curDate)) {
				if (i != 0) {
					sb.append("<tr><td colspan='" + COLUMN_NUM
							+ "'>&nbsp;</td></tr>");
					sb.append(FileUtils.LINE_SEPARATOR);
				}
				// 输出yyyy年MM月dd日 星期E
				sb.append("<tr><th colspan='" + COLUMN_NUM 
						+ "' class='region-title'>" + curDate
						+ "</th></tr>");
				sb.append(FileUtils.LINE_SEPARATOR);
				l = 0;
			}

			// even or odd line
			if (l % 2 == 0) {
				trClass = "even";
			} else if (l % 2 == 1) {
				trClass = "odd";
			}

			// focus
			if (match.getTypeFocused() == 1) {
				typeName = "<b>" + match.getTypeName() + "</b>";
			} else {
				typeName = match.getTypeName();
			}
			if (match.getTeam1Focused() == 1) {
				team1Name = "<b>" + match.getTeam1Name() + "</b>";
			} else {
				team1Name = match.getTeam1Name();
			}
			if (match.getTeam2Focused() == 1) {
				team2Name = "<b>" + match.getTeam2Name() + "</b>";
			} else {
				team2Name = match.getTeam2Name();
			}
			setChannelStr(match);
			
			String baiduShare = MatchUtils.getBaiduShareShort(match);
			sb.append(
					"<tr class='" + trClass + "'><td>"
					+ baiduShare + "&nbsp;&nbsp;&nbsp;&nbsp;" 
					+ typeName + "&nbsp;&nbsp;"
					+ team1Name);
			if (!StringUtils.isEmpty(team2Name)) {
				sb.append("&nbsp;-&nbsp;" + team2Name);
			}
			sb.append("&nbsp;&nbsp;&nbsp;&nbsp;" 
					+ match.getChannelStr()
					+ "</td></tr>");
			sb.append(FileUtils.LINE_SEPARATOR);
			prevDate = curDate;
		}
		sb.append("</table>");
		sb.append(FileUtils.LINE_SEPARATOR);
		return sb.toString();
	}
	
	private void setChannelStr(SoccerMatch match) {
		if (StringUtils.isEmpty(match.getChannelIds()))
			return;
		List<LiveChannel> list = liveChannelBo.getAllLiveChannels();
		List<String> channelStr = new ArrayList<String>();
		Set<Integer> channelIds = match.getChannelIdSet();
		for (Integer channelId : channelIds) {
			for (LiveChannel lc : list) {
				if (lc.getChannelId().equals(channelId)) {
					channelStr.add("<a target='_blank' href='" + lc.getUrl()
							+ "' " + " style='text-decoration:underline'>" + lc.getChannelName() + "</a>");
				}
			}
		}
		match.setChannelStr(StringUtils.join(channelStr, "&nbsp;&nbsp;&nbsp;&nbsp;"));
	}
}
